$(function(){
  getShow()
})

var allObj = {
  find:{},
  sort:{}
}

var dataAll = [];

$(".add").click(function(){
  var user = $(".user").val();
  var age = $(".age").val();
  var sex = $(":radio:checked").val();
  var sel = $("select").val();
  var isRed = $(":radio[name=isRed]:checked").val();

  if(!user || !age ){
    alert("请完善信息");
    return;
  }

  $.post("/add",{user,age,sex,sel,time:getTime(),isRed},function(res){
    if(res.code == 200){
      getShow();
      $(".user").val("");
      $(".age").val("");
      $(":radio:first").prop("checked",true);
      $("select").val("0");
      $(":radio[name=isRed]:last").prop("checked",true);
    }else{
      alert(res.msg);
    }
  })
})

function getShow(){
  $.get("/show",allObj,function(res){
    if(res.code == 200){
      show(res.info);
      dataAll = res.info;
    }else{
      alert(res.msg);
    }
  })
}

function show(arr){
  if(arr.length){
    $(".xuan").show();
    $(".tip").hide();
    var str = "";
    $.each(arr,function(i,n){
      str += `
        <tr>
          <td>${i+1}</td>
          <td class=${n.isRed == "true"?"active":""}>${n.user}</td>
          <td>${n.age}</td>
          <td>${n.sex == "0"?"男":"女"}</td>
          <td>${getSel(n.sel)}</td>
          <td>${n.time}</td>
          <td>${n.isRed}</td>
          <td>
          <button type="button" ${i?"":"disabled"} onclick="move(this,'${n._id}')">上移</button>
          <button type="button" ${i == arr.length - 1?"disabled":""} onclick="move(this,'${n._id}')">下移</button>
          <button type="button" onclick="del('${n._id}')">删除</button>
          </td>
        </tr>
      `;
    })
    $("tbody").html(str);
  }else{
    $(".xuan").hide();
    $(".tip").show();
  }
  
}

function getTime(){
  var d = new Date();
  return d.getFullYear()+'年'+
      numFn(d.getMonth()+1)+'月'+
      numFn(d.getDate())+'日 '+
      numFn(d.getHours())+':'+
      numFn(d.getMinutes())+':'+
      numFn(d.getSeconds())
}

function numFn(num){
  return num<10?"0"+num:num;
}

function del(id){
  $.get("/del",{id},function(res){
    if(res.code == 200){
      getShow()
    }else{
      alert(res.msg)
    }
  })
}

function getSel(i){
  var selArr = ['敲代码','打喜爱福','看电视','打扑克','看电影']
  return selArr[i];
}

$(".delAll").click(function(){
  $.get("/delAll",function(res){
    if(res.code == 200){
      getShow()
    }else{
      alert(res.msg)
    }
  })
})

$(".paixu").click(function(){
  allObj.sort={
    age:1
  }
  getShow()
})

$(".searchBtn").click(function(){
  if($(".search").val()){
    allObj.find = {
      $or:[
        {user:$(".search").val()},
        {age:$(".search").val()}
      ]
    }
  }else{
    allObj.find = {}
  }
  $(".search").val("")
  getShow()
})


function move(self,id){
  var index = dataAll.findIndex(function(item){
    return item._id == id;
  })
  var obj = dataAll[index];
  if($(self).html() == "上移"){
    dataAll[index] = dataAll[index-1];
    dataAll[index-1] = obj;
  }else{
    dataAll[index] = dataAll[index+1];
    dataAll[index+1] = obj;
  }
  show(dataAll);
}

